package com.shanzmoo.merchant.server.api.web.sys;

import cn.hutool.core.util.RandomUtil;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.util.RedisUtil;
import com.shanzmoo.merchant.server.api.domain.bo.*;
import com.shanzmoo.merchant.server.api.domain.vo.AuthUserVo;
import com.shanzmoo.merchant.server.api.manager.JwtManager;
import com.shanzmoo.merchant.server.api.manager.MemberInfoManage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@Api(tags = "系统登录")
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class LoginController {
    private final JwtManager jwtManager;
    private final MemberInfoManage memberInfoManage;

    @ApiOperation(value = "平台微信登录")
    @PostMapping("/ptwx/login")
    public SHttpResponse<AuthUserVo> ptwxLogin(@Valid @RequestBody LoginReqWeixinBO loginReq) throws Exception {
        String openId = memberInfoManage.getWxOpenId(loginReq.getWxCode());
        return SHttpResponse.ok(memberInfoManage.getAuthUserLogin(0,null,null,openId,null));
    }
    @ApiOperation(value = "平台支付宝登录")
    @PostMapping("/ptzfb/login")
    public SHttpResponse<AuthUserVo> ptzfbLogin(@Valid @RequestBody LoginReqZhifubaoBO loginReq) {
        String zfbUserId = memberInfoManage.getAlipayUserId(loginReq.getZfbCode());
        return SHttpResponse.ok(memberInfoManage.getAuthUserLogin(0,null,null,null,zfbUserId));
    }

    @ApiOperation(value = "商户微信登录")
    @PostMapping("/shwx/login")
    public SHttpResponse<AuthUserVo> shwxLogin(@Valid @RequestBody LoginReqWeixinBO loginReq) throws Exception{
        String openId = memberInfoManage.getWxOpenId(loginReq.getWxCode());
        return SHttpResponse.ok(memberInfoManage.getAuthUserLogin(2,null,null,openId,null));
    }

    @ApiOperation(value = "商户支付宝登录")
    @PostMapping("/shzfb/login")
    public SHttpResponse<AuthUserVo> shzfbLogin(@Valid @RequestBody LoginReqZhifubaoBO loginReq) {
        String zfbUserId = memberInfoManage.getAlipayUserId(loginReq.getZfbCode());
        return SHttpResponse.ok(memberInfoManage.getAuthUserLogin(2,null,null,null,zfbUserId));
    }
    @ApiOperation(value = "商户手机号登录")
    @PostMapping("/shmobile/login")
    public SHttpResponse<AuthUserVo> shmobileLogin(@Valid @RequestBody LoginReqMobileBO loginReq) {
        return SHttpResponse.ok(memberInfoManage.getAuthUserLogin(2,loginReq.getMobile(),loginReq.getCode(),null,null));
    }
    @ApiOperation(value = "用户注册")
    @PostMapping("/reg")
    public SHttpResponse<AuthUserVo> reg(@RequestBody LoginRegBo regBo) throws Exception{
        return SHttpResponse.ok(memberInfoManage.reg(regBo));
    }
    @ApiOperation(value = "手机验证码")
    @PostMapping("/phone-code")
    public SHttpResponse<String> phoneCode(@RequestBody LoginReqMobileCodeBO mobileCodeBO) throws Exception{
        String mobile = mobileCodeBO.getMobile();
        String phoneCode = RandomUtil.randomNumbers(6);
        RedisUtil.set("merchant-server-api:login:phone-code:"+mobile,phoneCode,30L);
        return SHttpResponse.ok(phoneCode);
    }
    @ApiOperation(value = "退出")
    @PostMapping("/logout")
    public SHttpResponse<Boolean> logout() {
        memberInfoManage.logout();
        return SHttpResponse.ok();
    }

}
